﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BizAppFactory.Domain;
using Microsoft.VisualStudio.Modeling;

namespace BizAppFactory.DomainModel.CustomEditors.UniqueConstraintEditor
{
    public partial class UniqueConstraintForm : Form
    {
        UniqueConstraint _editedUniqueConstraint;

        public UniqueConstraint EditedUniqueConstraint
        {
            get { return _editedUniqueConstraint; }
            set { _editedUniqueConstraint = value; }
        }

        Transaction transaction;
        
        public UniqueConstraintForm()
        {
            InitializeComponent();
        }

        private void UniqueConstraintForm_Load(object sender, EventArgs e)
        {
            Store store = EditorHelper.GetStore(EditedUniqueConstraint);
            transaction = store.TransactionManager.BeginTransaction("BindMethodData");
            InitializeAvailableAttributes();
            InitializeDataBinding();
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            transaction.Commit();
            Close();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            transaction.Rollback();
            Close();
        }

        BindingList<BizAppFactory.Domain.Attribute> availableAttributes;
        BindingList<BizAppFactory.Domain.Attribute> uniqueAttributes;

        void InitializeAvailableAttributes()
        {
            BindingList<BizAppFactory.Domain.Attribute> allPossibleAttribute = EditedUniqueConstraint.CollectAllPossibleAttributes();
            

            availableAttributes = new BindingList<BizAppFactory.Domain.Attribute>();
            foreach (BizAppFactory.Domain.Attribute attribute in allPossibleAttribute)
            {
                if (!EditedUniqueConstraint.Attributes.Contains(attribute))
                {
                    availableAttributes.Add(attribute);
                }
            }

            uniqueAttributes = new BindingList<BizAppFactory.Domain.Attribute>();

            foreach (BizAppFactory.Domain.Attribute attribute in allPossibleAttribute)
            {
                if (EditedUniqueConstraint.Attributes.Contains(attribute))
                {
                    uniqueAttributes.Add(attribute);
                }
            }

        }


        void InitializeDataBinding()
        {
            nameTextBox.DataBindings.Add("Text", EditedUniqueConstraint, "Name");
            displayNameTextBox.DataBindings.Add("Text", EditedUniqueConstraint, "DisplayName");
            descriptionTextBox.DataBindings.Add("Text", EditedUniqueConstraint, "Description");
            isNaturalKeyCheckBox.DataBindings.Add("Checked", EditedUniqueConstraint, "IsNaturalKey");

            uniqueAttributesListBox.DataSource = uniqueAttributes;
            uniqueAttributesListBox.DisplayMember = "Name";

            availableAttributesListBox.DataSource = availableAttributes;
            availableAttributesListBox.DisplayMember = "Name";
        }

        private void addAttributeButton_Click(object sender, EventArgs e)
        {
            BizAppFactory.Domain.Attribute selectedAttribute = (BizAppFactory.Domain.Attribute)availableAttributesListBox.SelectedItem;

            if (availableAttributesListBox.SelectedItem != null)
            {
                EditedUniqueConstraint.Attributes.Add(selectedAttribute);
                uniqueAttributes.Add(selectedAttribute);
                availableAttributes.Remove(selectedAttribute);
            }
        }

        private void removeAttributeButton_Click(object sender, EventArgs e)
        {
            BizAppFactory.Domain.Attribute selectedAttribute = (BizAppFactory.Domain.Attribute)uniqueAttributesListBox.SelectedItem;

            if (uniqueAttributesListBox.SelectedItem != null)
            {
                EditedUniqueConstraint.Attributes.Remove(selectedAttribute);
                uniqueAttributes.Remove(selectedAttribute);
                availableAttributes.Add(selectedAttribute);
            }

        }
    }
}
